function limitTextareaLine(e) {
    if(e.keyCode == 13 && $(this).val().split("\n").length >= $(this).attr('rows')) { 
        return false;
    }else
        return true;
}

function getTextAreaPosition(el) {

    var start = 0, end = 0, normalizedValue, range,
        textInputRange, len, endRange;

    if (typeof el.selectionStart == "number" && typeof el.selectionEnd == "number") {
        start = el.selectionStart;
        end = el.selectionEnd;
    } else {
        range = document.selection.createRange();

        if (range && range.parentElement() == el) {
            len = el.value.length;
            normalizedValue = el.value.replace(/\r\n/g, "\n");

            // Create a working TextRange that lives only in the input
            textInputRange = el.createTextRange();
            textInputRange.moveToBookmark(range.getBookmark());

            // Check if the start and end of the selection are at the very end
            // of the input, since moveStart/moveEnd doesn't return what we want
            // in those cases
            endRange = el.createTextRange();
            endRange.collapse(false);

            if (textInputRange.compareEndPoints("StartToEnd", endRange) > -1) {
                start = end = len;
            } else {
                start = -textInputRange.moveStart("character", -len);
                start += normalizedValue.slice(0, start).split("\n").length - 1;

                if (textInputRange.compareEndPoints("EndToEnd", endRange) > -1) {
                    end = len;
                } else {
                    end = -textInputRange.moveEnd("character", -len);
                    end += normalizedValue.slice(0, end).split("\n").length - 1;
                }
            }
        }
    }
    return start;
}
function setCharAt(str,index,chr){
   if(index > str.length-1) return str;
	return str.substr(0,index) + chr +''+ str.substr(index+1); 
}
function cek_materi_iklan_textarea(el){
    var batas_per_baris=5;
    var value=$(el).attr('value');
    if(limitTextareaLine(el)){
        var position=getTextAreaPosition(el);
        
        var find='';
        var jumlah_string=0;
        var index=position;
        while(find!='\n' && index!=0){
            find=value.charAt(index);
            index--;
            jumlah_string++;
        }
        
        index=position;
        while(find!='\n' && index!=value.length){
            find=value.charAt(index);
            index++;
            jumlah_string++;
        }
        console.log('jumlah karakter '+value.length+' jumlah string '+jumlah_string+' batas baris '+batas_per_baris);
        if(jumlah_string<=batas_per_baris){
            return true;
        }else{
            value=setCharAt(value, position-1, '');
            $(el).attr('value',value);
            
            console.log(jumlah_string+' '+value+' ');
            return false;
        }
    }else{
        return false;
    }
}
function window_popup(url){
    window.open(url, 'Kedaulatan Rakyat', 'width=800px, height=500px, scrollbars=1');
}
function numberToCurrency(a){
       if(a!=''&&a!=null){
       a=a.toString(); 
       var val=parseInt(a);
       var isNegatif=(val<0);
       if(isNegatif){
           a=val*(-1);
           a=a+'';
       }
        var b = a.replace(/[^\d\,]/g,'');
		var dump = b.split(',');
        var c = '';
        var lengthchar = dump[0].length;
        var j = 0;
        for (var i = lengthchar; i > 0; i--) {
                j = j + 1;
                if (((j % 3) == 1) && (j != 1)) {
                        c = dump[0].substr(i-1,1) + '.' + c;
                } else {
                        c = dump[0].substr(i-1,1) + c;
                }
        }
		
		if(dump.length>1){
			if(dump[1].length>0){
				c += ','+dump[1];
			}else{
				c += ',';
			}
		}
        if(isNegatif)
            return '-'+c;
        else
            return c;
        }
    else{
        return '0';
    }
}
function currencyToNumber(a){
    var b=a.toString();
    
    var c='';
    if(a!=null||a!=''){
        c=b.replace(/\.+/g, '');
    }
    
    return parseFloat(komaKeTitik(c));
}
function komaKeTitik(obj){
    var a=obj.toString();
    var b='';
    if(a!=null){
        b=a.replace(/\,/g,'.');
    }
    return b;
}
$(document).ready(function(){
    $('.uang').keyup(function(){
        $(this).attr('value',numberToCurrency($(this).val()));
    });
    $('body').delegate('.uang','keyup',function(){
        $(this).attr('value',numberToCurrency($(this).val()));
    });
    //gak mau jalan
    $('body').delegate('a[target=ajax-modal]','click',function(){
        alert('tes');
        $('.tanggal').datepicker();
    });
});